package cn.zymall.search.controller;

import cn.zymall.common.model.Spu;
import cn.zymall.common.utils.elsearch.ElSearchUtil;
import org.elasticsearch.cluster.metadata.AliasMetaData;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author xuxianda
 * @Description: 搜索引擎控制层
 * @date 2020/8/21 9:39
 * @Copyright
 * @return
 **/
@RestController
public class EsController {

    @Autowired
    ElSearchUtil elSearchUtil;

    /**
     * 搜索控制器
     * @param fieldName
     * @param keyword
     * @param classPath
     * @param pageable  传入 page，size，sort（默认desc，不知道为啥不生效了）
     * @return
     * @throws ClassNotFoundException
     */
    @GetMapping
    public List select(String fieldName, String keyword, String classPath,@PageableDefault(value = 15, direction = Sort.Direction.ASC)  Pageable pageable) throws ClassNotFoundException {
        return elSearchUtil.select(fieldName, keyword,Class.forName("cn.zymall.common.model.Spu"),pageable );
    }

}
